package com.ruoyi.project.system.stream.service.impl;

import java.util.List;

import com.ruoyi.project.system.shiping.domain.Shiping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.project.system.stream.mapper.StreamMapper;
import com.ruoyi.project.system.stream.domain.Stream;
import com.ruoyi.project.system.stream.service.IStreamService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 视频直播流Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class StreamServiceImpl implements IStreamService 
{
    @Autowired
    private StreamMapper streamMapper;

    /**
     * 查询视频直播流
     * 
     * @param id 视频直播流主键
     * @return 视频直播流
     */
    @Override
    public Stream selectStreamById(Long id)
    {
        return streamMapper.selectStreamById(id);
    }

    /**
     * 查询视频直播流列表
     * 
     * @param stream 视频直播流
     * @return 视频直播流
     */
    @Override
    public List<Stream> selectStreamList(Stream stream)
    {
        return streamMapper.selectStreamList(stream);
    }

    /**
     * 新增视频直播流
     * 
     * @param stream 视频直播流
     * @return 结果
     */
    @Transactional
    @Override
    public int insertStream(Stream stream)
    {
        int rows = streamMapper.insertStream(stream);
        insertShiping(stream);
        return rows;
    }

    /**
     * 修改视频直播流
     * 
     * @param stream 视频直播流
     * @return 结果
     */
    @Transactional
    @Override
    public int updateStream(Stream stream)
    {
        streamMapper.deleteShipingByShipingId(stream.getId());
        insertShiping(stream);
        return streamMapper.updateStream(stream);
    }

    /**
     * 批量删除视频直播流
     * 
     * @param ids 需要删除的视频直播流主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStreamByIds(String ids)
    {
        streamMapper.deleteShipingByShipingIds(Convert.toStrArray(ids));
        return streamMapper.deleteStreamByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除视频直播流信息
     * 
     * @param id 视频直播流主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStreamById(Long id)
    {
        streamMapper.deleteShipingByShipingId(id);
        return streamMapper.deleteStreamById(id);
    }

    /**
     * 新增视频管理信息
     * 
     * @param stream 视频直播流对象
     */
    public void insertShiping(Stream stream)
    {
        List<Shiping> shipingList = stream.getShipingList();
        Long id = stream.getId();
        if (StringUtils.isNotNull(shipingList))
        {
            List<Shiping> list = new ArrayList<Shiping>();
            for (Shiping shiping : shipingList)
            {
                shiping.setShipingId(id);
                list.add(shiping);
            }
            if (list.size() > 0)
            {
                streamMapper.batchShiping(list);
            }
        }
    }
}
